﻿using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    [DataContract]
    public class WArea : NotificationObject
    {
        private int areaUID;
        private String areaName;
        private int personnelUID;
        private WPersonnel person;

        [DataMember(Order = 0)]
        public int AreaUID
        {
            get
            {
                return areaUID;
            }
            set
            {
                areaUID = value;
                RaisePropertyChanged("AreaUID");
            }
        }

        [DataMember(Order = 1)]
        public String AreaName
        {
            get
            {
                return areaName;
            }
            set
            {
                areaName = value;
                RaisePropertyChanged("AreaName");
            }
        }

        [DataMember(Order = 2)]
        public int PersonnelUID
        {
            get
            {
                return personnelUID;
            }
            set
            {
                personnelUID = value;
                RaisePropertyChanged("PersonnelUID");
            }
        }

        [DataMember(Order = 3)]
        public WPersonnel Person
        {
            get
            {
                return person;
            }
            set
            {
                person = value;
                RaisePropertyChanged("Person");
            }
        }

        public WArea()
        {
            AreaUID = -1;
            AreaName = "";
            PersonnelUID = -1;
            Person = new WPersonnel();
        }

        public WArea(LArea p)
        {
            AreaUID = p.AreaUID;
            AreaName = p.AreaName;
            PersonnelUID = p.PersonnelUID;
        }

        public static int GetAreaUID(String pAreaName)
        {
            HttpResponse res = FridgeConnector.PostMessage(EStudy.BD_Area, EMethod.SelectAll, "");

            if (res.Status != "Success")
            {
                return -1;
            }

            List<WArea> list = JSON.parse<List<WArea>>(res.Data1);
            foreach (var item in list)
            {
                if (item.AreaName.Trim() == pAreaName.Trim())
                {
                    return item.AreaUID;
                }
            }

            return -1;
        }

        public static List<WArea> GetAll()
        {
            HttpResponse res = FridgeConnector.PostMessage(EStudy.BD_Area, EMethod.SelectAll, "");
            return JSON.parse<List<WArea>>(res.Data1);
        }

        public static HttpResponse Insert(List<WArea> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.BD_Area, EMethod.Insert, postData);
        }

        public static HttpResponse Update(List<WArea> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.BD_Area, EMethod.Update, postData);
        }

        public static HttpResponse Delete(List<WArea> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.BD_Area, EMethod.Delete, postData);
        }
    }
}
